package com.springboot.resttemplate.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;

@Service
@Slf4j
public class RetryService{
    @Resource
    private RestTemplate restTemplate;
    int retryCount = 1;
    @Async
    @Retryable(value = {Exception.class},maxAttempts = 3,backoff = @Backoff(delay = 2000))
    public CompletableFuture<String> retryMethod(){
        log.info("次数"+retryCount++);
         String result = restTemplate.getForObject("http://localhost:9090/user/retry",String.class);
         log.info("result:"+result);
        return CompletableFuture.completedFuture("success");
    }

    @Recover
    public CompletableFuture<String> recoverMethod(Exception e){
        log.info("服务降级");
        return CompletableFuture.completedFuture("recover");
    }
}